package cetcbigdata.da.business_environment.sichuan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.legal_person_service.sichuan.SCUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class SiChuan extends Base {

    private static String baseRegion = "四川省";

    private static String baseModule = "营商环境";

    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                List<JSONObject> module = getModule(r);
                for (JSONObject m : module) {
                    this.parseListPage(recordSender, fieldsList, r, m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem) {
        //获取区名和类型名称
        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name");
        //获取各个市的区和类型编码
        String regCode = regionItem.getString("regCode");
        String code = moduleItem.getString("code");
        int pageno = 1;
        try {
            //没有数据返回时，网站中会出现 div[@class='noitem'] 元素，则停止翻页
            while (true){
                String listUrl = String.format("http://www.sczwfw.gov.cn/jiq/interface/item/tags?dxType=3&areaCode=%s&type=0&pageno=%s&cycle=%s", regCode, pageno, code);
                JSONObject header = new JSONObject();
                JSONObject params = new JSONObject();

                String response = getReq(listUrl, header, params);
                JXDocument jxDocument = new JXDocument(response);
                List<Object> firstTitleList = jxDocument.sel("//div[@class='sx_list']");
                for (Object o : firstTitleList){
                    JSONObject content = new JSONObject();
                    String detailUrl = null;
                    String detailResponse = null;
                    String firstTitle = null;
                    List<JSONObject> xuanxiang = new ArrayList<>();
                    // 判断是否有子选项
                    Elements elements = ((Element) o).getElementsByTag("ul");
                    if(elements.size() > 0){
                        // 有子选项
                        Elements lis = ((Element) o).child(1).children();
                        for(Element li : lis){
                            JSONObject xuanxiangItem = new JSONObject();
                            String secondTitle = li.child(0).attr("title");
                            //获取详情页网址
                            String attr = li.attr("data-dirlistcode");
                            String taskInfoUrl = String.format("http://www.sczwfw.gov.cn/jiq/interface/index/taskInfo?jsonpCallback=jsonpcallback4&areaCode=%s&sortId=%s&userType=2&fromLevel=3&areaLevel=3&dirListCode=%s", regCode, code, attr);
                            JSONObject taskHeader = new JSONObject();
                            JSONObject taskParams = new JSONObject();
                            String taskInfoRs = getReq(taskInfoUrl, taskHeader, taskParams);
                            String data = taskInfoRs.replace("jsonpcallback4", "").replace("(", "").replace(")", "");
                            JSONObject taskInfoObj = JSONObject.parseObject(data);
                            JSONArray rsList = taskInfoObj.getJSONObject("data").getJSONArray("resultList");
                            for (Object r : rsList) {
                                JSONObject rr = (JSONObject) r;
                                String itemCode = rr.getJSONObject("curItemInfo").getString("eventCode");
                                String deptCode = rr.getJSONObject("curItemInfo").getString("deptCode");
                                detailUrl = String.format("http://www.sczwfw.gov.cn/jiq/front/transition/ywTransToDetail?areaCode=%s&itemCode=%s&taskType=1&deptCode=%s", regCode, itemCode, deptCode);
                            }
                            xuanxiangItem.put("名称", secondTitle);
                            xuanxiangItem.put("网址", detailUrl);
                            xuanxiang.add(xuanxiangItem);
                        }
                    }
                    else {
                        JSONObject xuanxiangItem = new JSONObject();
                        String name = ((Element) o).child(0).child(0).attr("title");

                        //获取详情页网址
                        String attr = ((Element) o).child(0).attr("data-dirlistcode");
                        String taskInfoUrl = String.format("http://www.sczwfw.gov.cn/jiq/interface/index/taskInfo?jsonpCallback=jsonpcallback4&areaCode=%s&sortId=%s&userType=2&fromLevel=3&areaLevel=3&dirListCode=%s", regCode, code, attr);
                        JSONObject taskHeader = new JSONObject();
                        JSONObject taskParams = new JSONObject();
                        String taskInfoRs = getReq(taskInfoUrl, taskHeader, taskParams);
                        String data = taskInfoRs.replace("jsonpcallback4", "").replace("(", "").replace(")", "");
                        JSONObject taskInfoObj = JSONObject.parseObject(data);
                        JSONArray rsList = taskInfoObj.getJSONObject("data").getJSONArray("resultList");
                        for (Object r : rsList) {
                            JSONObject rr = (JSONObject) r;
                            String itemCode = rr.getJSONObject("curItemInfo").getString("eventCode");
                            String deptCode = rr.getJSONObject("curItemInfo").getString("deptCode");
                            detailUrl = String.format("http://www.sczwfw.gov.cn/jiq/front/transition/ywTransToDetail?areaCode=%s&itemCode=%s&taskType=1&deptCode=%s", regCode, itemCode, deptCode);
                        }
                        xuanxiangItem.put("名称", name);
                        xuanxiangItem.put("网址", detailUrl);
                        xuanxiang.add(xuanxiangItem);
                    }
                    firstTitle = ((Element) o).child(0).child(0).attr("title");
                    content.put("名称", firstTitle);
                    content.put("选项", xuanxiang);
                    processResult(
                            listUrl, response, detailUrl, "", content, region, module
                    );
                }
                if (jxDocument.sel("//div[@class='sx_list']").size() == 0){
                    break;
                }
                pageno ++;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }


    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{ name: '阿坝藏族羌族自治州', value: '513200000000', areaId: '949' },\n" +
                "{ name: '巴中市', value: '511900000000', areaId: '1995' },\n" +
                "{ name: '成都市', value: '510100000000', areaId: '1447' },\n" +
                "{ name: '达州市', value: '511700000000', areaId: '504' },\n" +
                "{ name: '德阳市', value: '510600000000', areaId: '1499' },\n" +
                "{ name: '甘孜藏族自治州', value: '513300000000', areaId: '1512' },\n" +
                "{ name: '广安市', value: '511600000000', areaId: '1709' },\n" +
                "{ name: '广元市', value: '510800000000', areaId: '408' },\n" +
                "{ name: '乐山市', value: '511100000000', areaId: '890' },\n" +
                "{ name: '凉山彝族自治州', value: '513400000000', areaId: '329' },\n" +
                "{ name: '泸州市', value: '510500000000', areaId: '774' },\n" +
                "{ name: '眉山市', value: '511400000000', areaId: '1419' },\n" +
                "{ name: '绵阳市', value: '510700000000', areaId: '1746' },\n" +
                "{ name: '内江市', value: '511000000000', areaId: '1588' },\n" +
                "{ name: '南充市', value: '511300000000', areaId: '1199' },\n" +
                "{ name: '攀枝花市', value: '510400000000', areaId: '386' },\n" +
                "{ name: '遂宁市', value: '510900000000', areaId: '1477' },\n" +
                "{ name: '雅安市', value: '511800000000', areaId: '1028' },\n" +
                "{ name: '宜宾市', value: '511500000000', areaId: '1996' },\n" +
                "{ name: '资阳市', value: '512000000000', areaId: '1011' },\n" +
                "{ name: '自贡市', value: '510300000000', areaId: '661' }\n" +
                "]";
        return JSONArray.parseArray(city);
    }

    public static List<JSONObject> getRegion(){
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("value");


            //请求获取各市包含的区的名称和编码
            String url = "http://www.sczwfw.gov.cn/jiq/interface/jitem/find-by-code?code=" + code + "&deptType=1&jsonpCallback=jsonpcallback&_=1664773969890";
            JSONObject header = new JSONObject();
            JSONObject params = new JSONObject();

            String response = getReq(url, header, params);
            //处理数据获取区名和编码
            String data = response.replace("jsonpcallback", " ").replace("(", " ").replace(")", " ");
            JSONObject responseObj = JSONObject.parseObject(data);
            if(responseObj.getString("code").equals("200")){
                JSONArray resList = responseObj.getJSONObject("data").getJSONObject("group").getJSONArray("sonAreas");
                for (Object r : resList){
                    JSONObject rr = (JSONObject) r;
                    JSONObject outItem = new JSONObject();
                    String name = oo.getString("name") + "-" + rr.getString("name");
                    outItem.put("name", name);
                    outItem.put("regCode", rr.getString("areaCode"));
                    out.add(outItem);
                }
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(JSONObject r){
        List<JSONObject> out = new ArrayList<>();

        //请求类型地址
        String url = "http://www.sczwfw.gov.cn/jiq/front/item/fr_index?areaCode=" + r.getString("regCode");
        JSONObject header = new JSONObject();
        JSONObject params = new JSONObject();
        String response = getReq(url, header, params);
        JXDocument document = new JXDocument(response);
        List<Object> rs = document.sel("//div[@class='zq_k cf'] | //div[@class='zq_k']");
        for (Object o : rs) {
            JSONObject outItem = new JSONObject();
            String cycle = ((Element) o).child(0).attr("data-cycle");
            String name = ((Element) o).child(0).child(1).text();
            outItem.put("code", cycle);
            outItem.put("name", name);
            out.add(outItem);
        }
        return out;
    }

    public static void main(String[] args) {
        SiChuan sipder = new SiChuan();
        sipder.getData(null, new ArrayList<>());
    }
}
